package com.stop.think.shiro.service.paypal;

import com.paypal.core.PayPalEnvironment;
import com.paypal.core.PayPalHttpClient;
import com.stop.think.shiro.common.PayPalConfig;
import com.stop.think.shiro.common.ProjectConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class PayPalClient {

  private static PayPalHttpClient client;

  private PayPalClient() {}

  public static PayPalHttpClient client() {
    if (client != null) {
      return client;
    }
    PayPalEnvironment environment = !ProjectConfig.IS_TEST ? new PayPalEnvironment.Live(PayPalConfig.CLIENT_ID, PayPalConfig.CLIENT_SECRET)
            : new PayPalEnvironment.Sandbox(PayPalConfig.CLIENT_ID, PayPalConfig.CLIENT_SECRET);
    client = new PayPalHttpClient(environment);
    return client;
  }
}
